package com.chuangke.common.utils;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.core.util.StrUtil;

/**
 * IP相关工具类
 */
public class HttpServletRequestUtils {
    private static final Logger logger = LoggerFactory.getLogger(HttpServletRequestUtils.class);

    /**
     * 获取IP地址
     * <p>
     * 使用Nginx等反向代理软件， 则不能通过request.getRemoteAddr()获取IP地址
     * 如果使用了多级反向代理的话，X-Forwarded-For的值并不止一个，而是一串IP地址，X-Forwarded-For中第一个非unknown的有效IP字符串，则为真实IP地址
     */
    public static String getIpAddr(HttpServletRequest request) {
        if (request == null) {
            return null;
        }
        String ip = null;
        try {
            ip = request.getHeader("x-forwarded-for");
            if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (StrUtil.isBlank(ip) || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } catch (Exception e) {
            logger.error("IPUtils ERROR ", e);
        }

//        //使用代理，则获取第一个IP地址
//        if(StrUtil.isBlank(ip) && ip.length() > 15) {
//			if(ip.indexOf(",") > 0) {
//				ip = ip.substring(0, ip.indexOf(","));
//			}
//		}

        return ip;
    }

    public static String getOs(HttpServletRequest request) {
        if(request == null){
            return "";
        }

        String os = "";

        if (request.getHeader("User-Agent").toLowerCase().contains("windows")) {
            os = "Windows";
        } else if (request.getHeader("User-Agent").toLowerCase().contains("mac")) {
            os = "Mac";
        } else if (request.getHeader("User-Agent").toLowerCase().contains("x11")) {
            os = "Unix";
        } else if (request.getHeader("User-Agent").toLowerCase().contains("android")) {
            os = "Android";
        } else if (request.getHeader("User-Agent").toLowerCase().contains("iphone")) {
            os = "IPhone";
        } else {
            os = "UnKnown, More-Info: " + request.getHeader("User-Agent");
        }
        return os;
    }

    public static String getBrowser(HttpServletRequest request) {
        if(request == null){
            return "";
        }
        String user = request.getHeader("User-Agent").toLowerCase();

        String browser = "";

        if (user.contains("edge")) {
            browser = (request.getHeader("User-Agent").substring(request.getHeader("User-Agent").indexOf("Edge")).split(" ")[0]).replace("/", "-");
        } else if (user.contains("msie")) {
            String substring = request.getHeader("User-Agent").substring(request.getHeader("User-Agent").indexOf("MSIE")).split(";")[0];
            browser = substring.split(" ")[0].replace("MSIE", "IE") + "-" + substring.split(" ")[1];
        } else if (user.contains("safari") && user.contains("version")) {
            browser = (request.getHeader("User-Agent").substring(request.getHeader("User-Agent").indexOf("Safari")).split(" ")[0]).split("/")[0] + "-"
                    + (request.getHeader("User-Agent").substring(request.getHeader("User-Agent").indexOf("Version")).split(" ")[0]).split("/")[1];
        } else if (user.contains("opr") || user.contains("opera")) {
            if (user.contains("opera")) {
                browser = (request.getHeader("User-Agent").substring(request.getHeader("User-Agent").indexOf("Opera")).split(" ")[0]).split("/")[0] + "-"
                        + (request.getHeader("User-Agent").substring(request.getHeader("User-Agent").indexOf("Version")).split(" ")[0]).split("/")[1];
            } else if (user.contains("opr")) {
                browser = ((request.getHeader("User-Agent").substring(request.getHeader("User-Agent").indexOf("OPR")).split(" ")[0]).replace("/", "-"))
                        .replace("OPR", "Opera");
            }

        } else if (user.contains("chrome")) {
            browser = (request.getHeader("User-Agent").substring(request.getHeader("User-Agent").indexOf("Chrome")).split(" ")[0]).replace("/", "-");
        } else if ((user.contains("mozilla/7.0")) || (user.contains("netscape6"))
                || (user.contains("mozilla/4.7")) || (user.contains("mozilla/4.78"))
                || (user.contains("mozilla/4.08")) || (user.contains("mozilla/3"))) {
            browser = "Netscape-?";

        } else if (user.contains("firefox")) {
            browser = (request.getHeader("User-Agent").substring(request.getHeader("User-Agent").indexOf("Firefox")).split(" ")[0]).replace("/", "-");
        } else if (user.contains("rv")) {
            String IEVersion = (request.getHeader("User-Agent").substring(request.getHeader("User-Agent").indexOf("rv")).split(" ")[0]).replace("rv:", "-");
            browser = "IE" + IEVersion.substring(0, IEVersion.length() - 1);
        } else {
            browser = "UnKnown, More-Info: " + request.getHeader("User-Agent");
        }

        return browser;
    }
}
